package cn.dark.auth.core.repository;

import cn.dark.auth.core.repository.LoginRepository;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author dark
 * @date 2023-10-27
 */
public class DefaultLoginRepository implements LoginRepository {

    /**
     * 数据存储
     */
    private static final Map<Object, Object> dataMap = new ConcurrentHashMap<>();

    /**
     * 过期时间存储
     */
    private static final Map<Object, Integer> expireMap = new ConcurrentHashMap<>();

    @Override
    public void saveObject(Object key, Object value, int timeout) {
        if (timeout == 0) {
            return;
        }

        dataMap.put(key, value);

        expireMap.put(key, timeout);
    }

    @Override
    public Object getObject(Object key) {

        return dataMap.get(key);
    }

    @Override
    public int getTimeout(Object key) {
        return expireMap.get(key);
    }

    @Override
    public boolean hasKey(Object key) {
        return dataMap.containsKey(key);
    }

    @Override
    public void removeObject(Object key) {
        dataMap.remove(key);

        expireMap.remove(key);
    }

    @Override
    public void batchRemoveObject(List<Object> keys) {
        keys.forEach(this::removeObject);
    }
}
